package process_demo.review.about_timer;

/**
 * @author haomin
 * @date 2022/09/12 15:21
 **/
public class Timer2 {
    static class MyThread extends Thread{
        private Runnable task;
        private long delay;

        MyThread(Runnable task, long delay){
            this.task = task;
            this.delay = delay;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(delay);
                task.run();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        Runnable task = new Runnable() {
            @Override
            public void run() {
                // 多次执行方法一 while
                while (true){
                    try {
                        System.out.println("time is over");
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                // 多次执行方法二 再创建新的线程
//
//                try {
//                    System.out.println("time is over");
//                    Thread.sleep(3000);
//                    Thread t = new MyThread(task, 3000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }

            }
        };
        MyThread t = new MyThread(task, 3000);
        t.start();
    }
}